<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*本地api  对接*/
class LocalsApi extends  BaseApi
{
	protected $shop_id;//中未智联店铺id
	
	public function _initialize()
    {
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
	}
	/**
	 * http://SmartGYM.com/api/LocalsApi/GetContractInfo/card_no/123456/shop_id/22
	 *
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 */
	function GetContractInfo($card_no,$shop_id){
		$this->before_method($shop_id);
		$json=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->find();
		if(empty($json)){
			return template_error("卡号不存在(locals)");
		}else{
			return template_success("成功(locals)",array(
														  "ContractID"=>$json['contract_id'],
														  "MemberID"=>$json['member_id'],
														  "StartDate"=>$json['start_date'],
														   "MemberName"=>$json['name'],
														  "EndDate"=>$json['end_date'],
														  "ContractStatus"=>$json['contract_status'],
														  "Sex"=>0,
														  "Mobile"=>$json["mobile"],
														  "CardNo"=>$json['card_no'],
														  "MembershipType"=>$json['membership_type'],
														  "MembershipTypeName"=>"",
														  "SurplusNum"=>$json['surplus_num'],
														  "StoredSumValue"=>"0.00",
														  )
														);	
		}
	}
	/**
	 * 入场操作
	 * http://SmartGYM.com/api/LocalsApi/CheckIn/card_no/123456/shop_id/22
	 * @param $card_no
	 */
	function CheckIn($card_no,$shop_id){
	 	$model=db("Interface")->where("card_no='$card_no' and shop_id='$shop_id' and contract_status=1 ")->find();
		if(empty($model)){
			return template_error("无效的卡号(locals)");
		}else if($model['contract_status']!=1){
			return template_error("合同无效(locals)");
		}else if(strcmp($model['start_date'],date("Y-m-d"))>0||strcmp($model['end_date'],date("Y-m-d"))<0){
			return template_error("合同不再有效期内(locals)");
		}else if($model['membership_type']==1){//次卡
			if($model['surplus_num']<1){
				return template_error("卡剩余次数不足(locals)");
			}else{
				db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->setDec('surplus_num');
			}
		}
		return template_success("成功");
	}
	/**
	 * 出场操作
	 * @param $card_no
	 * @param $shop_id
	 * http://SmartGYM.com/api/LocalsApi/ConfirmCheckOut/card_no/123456/shop_id/22
	 */
	function ConfirmCheckOut($card_no,$shop_id){
		return template_success("成功(locals)");
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
